package com.go.ssh.commons.web.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.go.ssh.point.web.account.vo.AccountInfo;

public class AuthenticationFilter implements Filter {
	protected FilterConfig filterConfig = null;
	private String redirectURL = null;
	private String sessionKey = null;
	private List<String> notCheckURLList = new ArrayList<String>();
	private List<String> skipSuffixList = new ArrayList<String>();

	@Override
	public void destroy() {
		notCheckURLList.clear();
		skipSuffixList.clear();
	}

	@Override
	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession();
		String path = request.getServletPath();

		// if ("/".equals(path) || "/index".equalsIgnoreCase(path)) {
		// response.sendRedirect(request.getContextPath() + "/index");
		// return;
		// }

		// skip authentication
		if (sessionKey == null || isInNoFiltList(path) || isInSkipList(path)) {
			filterChain.doFilter(request, response);
			return;
		} else {
			// authentication
			if (authentication(session)) {
				filterChain.doFilter(request, response);
				return;
			} else {
				response.sendRedirect(request.getContextPath()
						+ this.redirectURL);
				return;
			}
		}
	}

	private boolean isInSkipList(String path) {
		if (path == null || skipSuffixList == null) {
			return false;
		}

		path = path.toLowerCase();
		for (int i = 0; i < skipSuffixList.size(); i++) {
			if (path.endsWith("." + skipSuffixList.get(i))) {
				return true;
			}
		}
		return false;
	}

	private boolean isInNoFiltList(String path) {
		if (path == null || notCheckURLList == null) {
			return false;
		}

		if (notCheckURLList.contains(path)) {
			return true;
		}
		return false;
	}

	private boolean authentication(HttpSession session) {
		AccountInfo user = (AccountInfo) session.getAttribute(sessionKey);
		return user != null;
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
		redirectURL = filterConfig.getInitParameter("redirectURL");
		sessionKey = filterConfig.getInitParameter("checkSessionKey");
		String notCheckURLListStr = filterConfig
				.getInitParameter("notCheckURLList");
		if (notCheckURLListStr != null) {
			String[] params = notCheckURLListStr.split(",");
			if (params != null && params.length > 0) {
				for (String param : params) {
					notCheckURLList.add(param.trim());
				}
			}
		}

		String skipSuffix = filterConfig.getInitParameter("skipSuffixes");
		if (skipSuffix != null) {
			skipSuffix = skipSuffix.toLowerCase();
			String[] skip = skipSuffix.split(",");
			if (skip != null && skip.length > 0) {
				for (String s : skip) {
					skipSuffixList.add(s.trim());
				}
			}
		}
	}
}
